const Result = require('./Result')
const jwt = require('jsonwebtoken')
const {login} = require('../services/user')
const {md5} = require('../utils')
const {PWD_SALT, PRIVATE_KEY, JWT_EXPIRED} = require('../utils/constant')

class UserCtl {
  login(req, res) {
    let {username, password} = req.body
    
    password = md5(`${password}${PWD_SALT}`)
    console.log(password)
    login(username, password).then(result => {
      console.log(result)
      if(!result || !result.length) {
        new Result('登录失败').success(res)
      } else {
        const token = jwt.sign({username}, PRIVATE_KEY, {expiresIn: JWT_EXPIRED})
        console.log(token)
        new Result({token},'登录成功').success(res)
      }
    }) 
    // querySql(`select * from admin_user where username='${username}' and password = '${password}'`).then(result => {
    // })

   
  }
}
module.exports = new UserCtl()